from django.http import HttpResponse
from django.shortcuts import render
from paginator_study.models import Student
from django.core.paginator import Paginator


# Create your views here.
# 生成分页数值列表[1,2,3,4,5,6,7,8,9]
def get_range_list(num, total_page, size=9):
    '''
    num:当前页
    total_page:总页数
    size:列表的总个数
    '''
    min = num - int(size / 2)
    min = min if min >= 1 else 1
    max = min + size - 1
    max = max if max <= total_page else total_page
    print(min, max + 1)
    return range(min, max + 1)


def students(request):
    student_obj = Student.objects.all()
    paginator_obj = Paginator(student_obj, per_page=10, orphans=6)

    number = request.GET['num']
    page_obj = paginator_obj.get_page(number)
    range_list = get_range_list(page_obj.number, paginator_obj.num_pages, size=9)
    return render(request, './paginator_demo/student_page.html',
                  {'paginator_obj': paginator_obj, 'page_obj': page_obj, 'range_list': range_list})
